package twentyOne;

import nineteen.Generator;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 测试IntGenerator
 *
 * @author sun.ye
 * @since 2020-05-04 11:07
 */
public class EvenChecker implements Runnable{

    private IntGenerator intGenerator;
    private final int ID;

    public EvenChecker(IntGenerator intGenerator, int ID) {
        this.intGenerator = intGenerator;
        this.ID = ID;
    }

    @Override
    public void run() {
        while (!intGenerator.isCanceled()){
            int val = intGenerator.next();
            if (val % 2 != 0){
                System.out.println(val + " not even !");
                intGenerator.cancel();
            }else {
                System.out.println("now value is " + val);
            }
        }
    }

    public static void test(IntGenerator generator) {
        test(generator,10);
    }

    public static void test(IntGenerator generator, int count)  {
        System.out.println("Press control-c to exit");
        ExecutorService service = Executors.newCachedThreadPool();
        for (int i = 0; i < count ; i++) {
            service.execute(new EvenChecker(generator,i));
        }
        service.shutdown();
    }

}
